﻿using System;
using System.Threading;
using CS.Beehive;
using CS.Diagnostics;
using CS.Net;

namespace CS.Demos
{
    public class StpServerDemo
    {
        public static void Run()
        {
            //初始化配置，不执行该步将使用默认配置
            //TcpFactory.Setup(10, 10, 8, 16);
            var server = TcpFactory.CreateTcpServer();
            //连入事件
            //server.Connecting += (s, e) => Tracer.Debug("[{0}] is connecting.", e.ClientSocket.RemoteEndPoint);
            server.Connected += (s, e) =>
                {
                    Tracer.Info("ClientId = {0} is connected.", e.Channel.ClientId);
                    e.Channel.DataReceived += (cs, ce) =>
                        {
                            var sender = s as ITcpChannel;
                            var len = ce.BytesTransferred;
                            string value = ce.Buffer.ToHexString(0, len > 20 ? 20 : len);
                            Tracer.InDebug("ClientId={0} had received:[{1}/{2}] UTF8:{3}", sender != null ? sender.ClientId : 0, len, ce.Buffer.Length, ce.Buffer.ToUtf8(0, len), value);
                            if (sender != null)
                            {
                                //Thread.Sleep(200);
                                sender.Send("Send from Server.".ToBytes());
                            }
                        };
                };
            //断开事件 
            //server.ClientDisConnecting += (s, e) => Tracer.Debug("[{0}] is connecting.",e.Channel.ClientId);
            server.ClientDisconnected += (s, e) => Tracer.Info("ClientId = {0} is disconnected.", e.Channel.ClientId);
            //TcpServer异常
            server.TcpException += (s, e) => Tracer.Error("Exception:{0}", e.TcpException); ;
            


            //侦听4444端口
            //server.Start("*",4444);
            server.Start(4444);
            Thread.Sleep(5000);
            server.SendToAll("sendtoall by server.服务器发给所有小伙伴。".ToBytes());
            while (true)
            {
                if (server.Connections == 0) continue;
                Tracer.Info("{4}  IN[{0}|{2}],OUT[{1}|{3}]", server.ReceiveIO, server.SendIO, server.ReceivedByteSize, server.SentByteSize, server.Connections);
                Thread.Sleep(10000);
            }

        }

        public static void RunClient()
        {
            for (int i = 0; i < 10; i++)
            {
                RunOneCase(null);
                //Thread.Sleep(500);
            }
        }

        public static void RunOneCase(object state)
        {
            try
            {
                var client = new MyClient();
                client.DataReceive += (s, e) =>
                {
                    var sender = s as ITcpChannel;
                    var len = e.BytesTransferred;
                    //string value = e.Buffer.ToHexString(0, len > 20 ? 20 : len);
                    Tracer.InDebug("ClientId={0} had received:[{1}/{2}] UTF8:{3}", sender != null ? sender.ClientId : 0, len, e.Buffer.Length, e.Buffer.ToUtf8(0, len));
                };
                //client.Trace += (s, e) => Tracer.Debug(e.Message);
                client.TcpException += (s, e) => Tracer.Error(e.TcpException);
                client.Connect("127.0.0.1", 4444);
                client.Send(string.Format("TEST_{0}", DateTime.Now).ToBytes());
                client.Disconnect();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex);
                //throw;
            }

        }

    }




    public class MyClient : TcpClient
    {
    }




}